//////////////////////////////////////////////////////////////////////////
#pragma once


//////////////////////////////////////////////////////////////////////////
#include <Core/Core.hpp>
#include <Config/Config.hpp>
#include <Config/SerializersMath.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <UI/Context.hpp>


//////////////////////////////////////////////////////////////////////////
// Forward references for Rocket.
namespace Rocket
{
	namespace Core
	{
		class ElementDocument;
	}
}


namespace Application
{
	//////////////////////////////////////////////////////////////////////////
	class Clock;
	class WindowManager;
	class RocketRenderInterface;


	//////////////////////////////////////////////////////////////////////////
	class GameWindow : public sf::RenderWindow
	{
	public:
		virtual ~GameWindow();

		//
		bool							Update();

		Rocket::Core::ElementDocument*	LoadDocument( const std::string& name );
		void							SetFullscreen( bool fs );

		// Hide base class implementations.
		void							close();

		// Overrides from sf::Window.
		virtual void					onCreate() override;
		virtual void					onResize() override;

		void							_UpdateSizePosition();

		//
		Math::Point2i&					Position()						{return mPosition;}
		void							Position( Math::Point2i& p )	{mPosition = p;}
		Math::Point2i&					Size()							{return mSize;}
		void							Size( Math::Point2i& s )		{mSize = s;}
		bool							Focused() const					{return mHasFocus;}

		//////////////////////////////////////////////////////////////////////////
		// Factory interface.
		typedef std::function< GameWindow* ( WindowManager*, sf::VideoMode, const sf::String&, sf::Uint32, const sf::ContextSettings& ) >
			Creator_t;
		static void						SetCreator( Creator_t c );
		static GameWindow*				Create(
			WindowManager*				winMgr,
			sf::VideoMode				mode,
			const sf::String&			title,
			sf::Uint32					style		=	sf::Style::Default,
			const sf::ContextSettings&	settings	=	sf::ContextSettings()
			);


		// Win32 specific, want to remember if the window was maximized.
#ifdef OS_WINDOWS
		bool							Maximized() const;
#endif

	private:
		GameWindow(
			WindowManager*				winMgr,
			sf::VideoMode				mode,
			const sf::String&			title,
			sf::Uint32					style		=	sf::Style::Default,
			const sf::ContextSettings&	settings	=	sf::ContextSettings()
			);

		void							_CreateWindow();

		//
		WindowManager*					mpWindowManager;
		std::string						mName;
		uint32_t						mToggleFullscreen;
		bool							mFullscreen;
		bool							mHasFocus;

		CONFIG_MEMBER( Math::Point2i,	mPosition );
		CONFIG_MEMBER( Math::Point2i,	mSize );
#ifdef OS_WINDOWS
		CONFIG_MEMBER( bool,			mMaximized );
		CONFIG_MEMBER( Math::Point2i,	mMaximizedPos );
#endif

		UI::Context						mContext;
		RocketRenderInterface*			mpRenderer;
		Application::Clock&				mClock;

		static Creator_t				sCreator;
	};
}
